"""
Case Type   : datakit安装
Case Name   : 【服务器管理】新增物理机后编辑物理机
Create At   : 2025/5/20
Owner       : @lonely-dance
Description :
    1.添加服务器时点击联通性测试
    2.添加服务器
    3.编辑新添加的物理机
    4.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
History     :
"""

import os
import re
import unittest

from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.datakit_api.CommonApi import CommonApi
from testcase.utils.CommonDatakit import CommonDatakit


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.com_sh = CommonSH()
        self.pri_node = Node('PrimaryDbUser')
        self.mysql_node = Node('mysql5')
        self.datakit_node = Node('Datakit_Root')
        self.token = self.com_sh.get_token_value(macro.DATAKIT_PASSWD)
        self.log.info(self.token)
        self.assertNotEqual("error", self.token)
        self.com_api = CommonApi(self.token)
        self.com_datakit = CommonDatakit(self.token)
        self.cluster_name = re.search(r'Case\d+', os.path.basename(__file__)).group()
        self.data1 = {"privateIp": f"{self.pri_node.ssh_host}", "publicIp": f"{self.pri_node.ssh_host}",
                      "port": 22, "password": f"{self.pri_node.ssh_password}",
                      "username": f"{self.pri_node.ssh_user}"}
        self.data2 = {"hostId": "",
                      "name": f"{self.pri_node.ssh_host}",
                      "password": f"{self.pri_node.ssh_password}",
                      "port": 22,
                      "privateIp": f"{self.pri_node.ssh_host}",
                      "publicIp": f"{self.pri_node.ssh_host}",
                      "remark": "",
                      "tags": [],
                      "username": f"{self.pri_node.ssh_user}"}
        self.data3 = {"hostId": "",
                      "name": f"{self.pri_node.ssh_host}",
                      "password": f"{self.pri_node.ssh_password}",
                      "port": 22,
                      "privateIp": f"{self.pri_node.ssh_host}",
                      "publicIp": f"{self.pri_node.ssh_host}",
                      "remark": "aaa",
                      "tags": [],
                      "username": f"{self.pri_node.ssh_user}"}

    def test_base_ops(self):
        text = f'----step1:添加服务器时点击联通性测试 expect：成功----'
        self.log.info(text)
        response = self.com_api.post_host_ping(self.data1)
        self.log.info(response.json())
        self.assertEqual(200, response.json()["code"], '执行失败' + text)

        text = f'----step2:添加服务器 expect：成功----'
        self.log.info(text)
        response = self.com_api.post_host(self.data2)
        self.log.info(response.json())

        text = f'----step3:编辑服务器 expect：成功----'
        self.log.info(text)
        response = self.com_api.get_host_page(name=f"{self.pri_node.ssh_host}")
        self.log.info(response.json())
        host_id = response.json()['rows'][0]['hostId']
        response = self.com_api.put_host(host_id=host_id, data=self.data3)
        self.log.info(response.json())
        self.assertEqual(200, response.json()['code'], '执行失败' + text)

    def tearDown(self):
        self.log.info('----step4:清理环境 expect:成功----')
        self.com_datakit.delete_host()
        self.log.info(f"-----{os.path.basename(__file__)} finsh-----")
